var app = getApp();

//API 根路径
const apiUrl = 'https://api.fredzhu.com/yi/';

function loadData(path, data, successf, method = "GET") {
  // console.log("loadData", app.globalData);
  data['_session_'] = app.globalData.session;
  data['_userId_'] = app.globalData.userId;
  wx.request({
    url: apiUrl + path,
    data: data,
    method: method,
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      successf(res);
    }
  });
}

function loadArticles(tp, pageIndex, pageSize, fun) {
  wx.showLoading({
    title: '加载中',
  })
  loadData(tp + '/' + pageSize + '/' + pageIndex, {}, function (res) {
    console.log("loadArticles", tp, pageIndex, pageSize, res);
    fun(res.data.list, res.data.count);
    wx.hideLoading()
  });
}


function updateUserInfo() {
  wx.getUserInfo({
    success: res => {
      console.log(res.userInfo);
      wx.setStorage({
        key: 'userInfo',
        data: res.userInfo
      })
      app.globalData.userInfo = res.userInfo
      loadData("/user/update", res.userInfo, function (res) {
        console.log("update success");
      });
    }
  })
}


module.exports = {
  loadData: loadData,
  loadArticles: loadArticles,
  updateUserInfo: updateUserInfo
}
